package main

import (
	"encoding/json"
	"fmt"
	"log"

	gapi "github.com/grafana/grafana-api-golang-client"
)

func main() {
	client, err := gapi.New("https://grafana-cloudgame.byted.org", gapi.Config{
		APIKey: "eyJrIjoiQjJ2WFJNU1dsQzJFN0RHdVVhSGlEeUNuQ3ZnM3lqaFoiLCJuIjoiY2FhcyIsImlkIjoxfQ==", // 替换为你的 Grafana API 密钥
	})
	if err != nil {
		log.Fatalf("Error creating Grafana client: %s", err)
	}
	hmacAuth := "v3/prometheus-ak/1718608192/1800000/direct/100.65.0.182:31080/http//false/21b333424f208bd3aecb3f98a2f721aa5752d91d35d0a09867cf0320f40d41c2"

	// 配置 Prometheus 数据源
	ds := gapi.DataSource{
		Name:      "boe-tjjnsx-platform-1",
		Type:      "prometheus",
		Access:    "proxy",
		Database:  "Prometheus",
		URL:       "https://ppe-tjjnsx.vegamews.volces.com/prometheus", // 替换为你的 Prometheus 服务器地址
		IsDefault: false,
		JSONData: map[string]interface{}{
			"httpMethod":      "GET",
			"httpHeaderName1": "Ace-Gateway-Proxy-Auth",
			"httpHeaderName2": "Ace-Gateway-Proxy-Version",
		},
		SecureJSONData: map[string]interface{}{
			"basicAuthUser":     "byted",        // 替换为你的用户名
			"basicAuthPassword": "Bytedance123", // 替换为你的密码
			"httpHeaderValue1":  hmacAuth,
			"httpHeaderValue2":  "v2",
		},
		BasicAuth:     true,
		BasicAuthUser: "byted",
		ReadOnly:      false,
	}

	did, err := client.NewDataSource(&ds)
	if err != nil {
		log.Fatalf("Error creating datasource: %s", err)
	}
	fmt.Println(did)

	dss, err := client.DataSources()
	if err != nil {
		log.Fatalf("Error creating datasource: %s", err)
	}

	for _, d := range dss {
		if d.Name == "boe-tjjnsx-platform-1" {
			ss, _ := json.Marshal(d)
			fmt.Println(string(ss))
			// client.DeleteDataSource(d.ID)
		}
	}
	client.DeleteDataSourceByName("boe-tjjnsx-platform-1")

	fmt.Println("Datasource created successfully")
}
